<?php

if (!defined('BASEPATH'))
	exit('No direct script access allowed');

/**
 * Description of MY_Sms_interface
 *
 * @author Doufu <442958506@qq.com>
 */
class Sms_interface {

	private $server = 'https://api.sms.mob.com';
	private $CI;

	public function __construct() {
		$this->CI = & get_instance();
		$configServer = config_item('SMS_SERVER');
		if(!empty($configServer)) {
			$this->server = $configServer;
		}
	}
	public function verify($mobile, $code, $timeout = 30) {

		$api = $this->server . '/sms/verify';
		$data = array(
			'appKey' => config_item('SMS_APPKEY'),
			'phone' => $mobile,
			'zone' => '86',
			'code' => $code
		);
		$response = $this->postRequest($api, $data, $timeout);
		$json = json_decode($response, true);
		if($json['status'] == 200) {
			return true;
		}
		return false;
	}
	
	public function postRequest($api, $params = array(), $timeout = 30) {
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, $api);
		// 以返回的形式接收信息
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		// 设置为POST方式
		curl_setopt($ch, CURLOPT_POST, 1);
		curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($params));
		// 不验证https证书
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
		curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
		curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
		curl_setopt($ch, CURLOPT_HTTPHEADER, array(
			'Content-Type: application/x-www-form-urlencoded;charset=UTF-8',
			'Accept: application/json',
		));
		// 发送数据
		$response = curl_exec($ch);
		// 不要忘记释放资源
		curl_close($ch);
		return $response;
	}
}
